<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @subpackage Renderer
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */


/** @see Zend_DataGrid_Renderer */
require_once 'Zend/DataGrid/Renderer.php';

/** @see Zend_Paginator */
require_once 'Zend/Paginator.php';


/**
 * Zend_DataGrid_Renderer_Html
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @subpackage Renderer
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Html.php 8862 2008-09-11 15:36:00Z azoungrana $
 */
class Zend_DataGrid_Renderer_Html extends Zend_DataGrid_Renderer
{


    /**
     * The pager component
     *
     * @var Zend_Paginator
     */
    protected $_pager = null;

    /**
     * Get the type of render (HTml, Xml....)
     * @return string
     */
    public function getType()
    {
        return 'Html';
    }

    /**
     * Initialize the renderer options
     *
     * @return Zend_DataGrid_Renderer
     */
    protected function initOptions()
    {
        parent::initOptions();

        $options = array( 'showFooter' => false,
                          'showHeader' => true,

                          'encoding' => 'ISO-8859-1',
                          'numberAlign' => true,
                          //'sortFieldsNum' => 3,
                          //'hideColumnLinks' => array(),
                          'textAscending' => 'Ascending',
                          'textDescending' => 'Descending',
                          'selfPath' => '',
                          'sortIconASC' => '',
                          'sortIconDESC' => '',

                          'attributes' => array(),
                          'headerAttributes' => array(),
                          'headerAscAttributes' => array(),
                          'headerDescAttributes' => array(),
                          'itemAttributes' => array(),
                          'alternatingItemAttributes' => array(),
                          'footerAttributes' => array(),

                          'jsOnMove' => null,
                          'jsOnMoveData' => null);

        $this->_options = array_merge($this->_options, $options);

        return $this;
    }


    /**
     * Whether to build the header
     *
     * @param boolean $value
     */
    public function setShowHeader($value)
    {
        $this->_options['showHeader'] = (boolean) $value;
    }

    /**
     * Whether to build the footer.
     *
     * @param boolean $value
     */
    public function setShowFooter($value)
    {
        $this->_options['showFooter'] = (boolean) $value;
    }

    public function setDefaultColumnValue($colomnName, $value)
    {

    }

    /**
     * Per-column default cell value. This is an array of the form: array(fieldName => value, ...).
     *
     * @param array $value
     */
    public function setDefaultColumnValues(array $value) {}

    /**
     * The content encoding. If the mbstring extension is present the default value is set from mb_internal_encoding(), otherwise it is ISO-8859-1.
     *
     * @param string $value
     */
    public function setEncoding($value) {}

    /**
     * By default sorting links are enabled on all columns. With this option it is possible to disable sorting
     * links on specific columns. This is an array of the form: array(fieldName, ...).
     * This option only affects drivers that support sorting.
     *
     * @param array $value
     */
    public function setHideColumnLinks(array $value) {}

    /**
     * Whether to right-align numeric values.
     *
     * @param boolean $value
     */
    public function setNumberAlign($value) {}

    /**
     * How many fields the user will be able to sort by. This has no effect if the backend does not support sorting by multiple fields.
     *
     * @param integer $value
     */
    public function setSortFieldsNum($value) {}

    /**
     * Label for the ASC direction
     *
     * @param string $value
     */
    public function setTextAscending($value) {}

    /**
     * 	Label for the DESC direction
     *
     * @param string $value
     */
    public function setTextDescending($value) {}

    /**
     * The complete path for sorting and paging links.
     *
     * @param string $value
     */
    public function setSelfPath($value) {}

    /**
     * The icon to define that sorting is currently ascending. Can be text or HTML to define an image.
     *
     * @param string $value
     */
    public function setSortIconASC($value) {}

    /**
     * The icon to define that sorting is currently descending. Can be text or HTML to define an image.
     *
     * @param string $value
     */
    public function setSortIconDESC($value) {}

    /**
     * Whether sorting HTTP queries reset paging.
     *
     * @param boolean $value
     */
    public function setSortingResetsPaging($value) {}


    //Style/class/attributes

    /**
     * Add attribute
     * @param string $name
     * @param string $value
     * @return Zend_DataGrid_Renderer_Html
     */
    public function setAttribute($name, $value)
    {
        $attributes = $this->getOption('attributes');
        $attributes[$name] = $value;
        $this->setOption('attributes', $attributes);

        return $this;
    }

    /**
     * 	Column cells attributes. This is an array of the form: array(fieldName => array(attribute => value, ...) ...)
     *
     * @param string $name
     * @param string $value
     */
    public function setAttributes($value)
    {
        if (is_array($value)) {
            $this->setOption('attributes', $value);
        } else {
            throw new Zend_DataGrid_Renderer_Exception('Must be an array');
        }
    }

    public function setHeaderAttribute($name, $value)
    {
        $this->_options['headerAttributes'][$name] = $value;
    }


    /**
     * Attributes for the header row. This is an array of the form: array(attribute => value, ...)
     *
     * @param string $name
     * @param string $value
     */
    public function setHeaderAttributes($value)
    {
        $this->setOption('headerAttributes', $value);
    }

    /**
     * A class/style/attributes name for TH elements to define that sorting is currently ascending
     *
     * @param string $name
     * @param string $value
     */
    public function setHeaderAscAttributes($name, $value) {}

    /**
     * A class/style/attributes name for TH elements to define that sorting is currently descending
     *
     * @param string $name
     * @param string $value
     */
    public function setHeaderDescAttributes($name, $value) {}

    /**
     * d
     *
     * @param string $name
     * @param string $value
     */
    public function setItemAttributes($name, $value) {}

    public function setAlternatingItemAttribute($name, $value)
    {
        $this->_options['alternatingItemAttributes'][$name] = $value;
    }


    /**
     * d
     *
     * @param string $name
     * @param string $value
     */
    public function setAlternatingItemAttributes($value) {
        $this->_options['alternatingItemAttributes'] = $value;
    }

    public function setFooterAttribute($name, $value)
    {
        $this->_options['footerAttributes'][$name] = $value;
    }

    /**
     * d
     *
     * @param string $name
     * @param string $value
     */
    public function setFooterAttributes($value)
    {
        $this->_options['footerAttributes'] = $value;
    }


    //pager


    public function setPagerOption($key, $value) {}

    public function getPagerOption($key) {}

    public function setPagerOptions(array $options) {}

    public function getPagerOptions() {}


    // events


    /**
     * Name of a Javascript function to call on onclick/onsubmit events when the user is
     * either paging or sorting the data. This function receives a single object argument
     * of the form: { page: <page>, sort: [{field: <field>, direction: <direction>}, ...],
     * data: <user_data> }. Remark: setting this option doesn't remove the href attribute,
     * you should return false from your handler function to void it (eg: for AJAX, etc..).
     *
     * @param string $value
     */
    public function setJsOnMove($value) {}

    /**
     * User data passed in the "data" member of the object argument passed to onMove.
     * No JSON serialization is performed, this is assigned as a raw string to the "data"
     * attribute. It's up to you to add quotes, slashes, etc...
     *
     * @param string $value
     */
    public function setJsOnMoveData($value) {}


    //rendering


    /**
     * Contruct html attributes and return a string
     *
     * @param array $attribs
     * @return string
     */
    protected function _htmlAttribs(array $attribs)
    {
        $content = '';
        $view = $this->getView();
        foreach ($attribs as $key => $val) {
            $key = $view->escape($key);
            if (is_array($val)) {
                $val = implode(' ', $val);
            }
            $val = $view->escape($val);
            $content .= " $key=\"$val\"";
        }
        return $content;
    }

    /**
     * Generates tree level if column contain tree level
     *
     * <example> The renderer will generate this column
     *  | text1
     *  | 	|- text2
     *  	|-	|- text3
     *  | text4
     *  | ...
     * </example>
     *
     * @param array $data
     * @return string
     */
    protected function generateTreeLevel($rowData)
    {
        $content = '';
        if ($tdf = $this->_gridElement->getTreeLevelDataField()) {
            $level = (int) $rowData[$tdf];
            while($level--)
                $content .= '<sup>.</sup>&nbsp;&nbsp;&nbsp;&nbsp;';
            if($content) $content .= "<sup>|_</sup>&nbsp;";
        }
        return $content;
    }

    /**
     * Renders the cell header for a column
     * Used by extended classes
     *
     * @return string
     */
    public function renderHeader()
    {
        $column = $this->_gridElement;

        $textHeader = $column->getHeaderText();
        if ((null !== $textHeader) && (null !== ($translator = $column->getTranslator()))) {
            $textHeader = $translator->translate($textHeader);
        }

        return $textHeader;
    }

    /**
     * Renders the cell item for a column
     * Used by extended classes
     *
     * @param mixed $data
     * @param boolean $isAlternatingItem when item is alternating
     * @return string
     */
    public function renderItem($rowData, $isAlternatingItem = false)
    {
        $column = $this->_gridElement;
        $formatString = $column->getDataFormat();
        $myData = array();
        $content = '';

        if (count($dataFields = $column->getDataFields())) {
            if (null == $formatString) {
                $formatString = '%s';
            }

            foreach ($dataFields as $df) {
                $myData[] = $rowData[$df];
            }
            $content = vsprintf($formatString, $myData);
        } else {
            $content = $formatString;
        }

        //filtrer le contenu
        $content = $column->filter($content);

        return $content;
    }

    /**
     * Renders the cell footer for a column
     * Used by extended classes
     *
     * @return string
     */
    public function renderFooter()
    {
        $column = $this->_gridElement;
        $textFooter = $column->getFooterText();
        if (!$textFooter) {
            $textFooter = $column->getHeaderText();
        }
        if ((null !== $textFooter) && (null !== ($translator = $column->getTranslator()))) {
            $textFooter = $translator->translate($textFooter);
        }

        $content = $textFooter;

        return $content;
    }

}




